<?php 
	require_once './utils/DBUtil.php';
	$searched = false;
	if(isset($_POST['mname'])){
		$mname = $_POST['mname'];
		$searched = true;
	}
	if(isset($_POST['myear'])){
		$myear = $_POST['myear'];
		$searched = true;
	}
	if(isset($_POST['mgenre'])){
		$mgenre = $_POST['mgenre'];
		$searched = true;
	}
?>
<html>
<head>
<title>Search Page</title>
  <link href="./css/header.css" rel="stylesheet">
  <link href="./css/index.css" rel="stylesheet">
</head>
<body>
<div class="pagetitle"><h1>Search</h1></div>
<div id="nav">
	<a href="./design.php">Database Design and Implementation</a>
	<a href="./index.php">Search</a>
</div>
<div class="desc">
<p>This is a search page and you can search by different conditions!</p>
</div>
<div class="search">
	<form action="<?php echo  $_SERVER['PHP_SELF']?>" method="POST">
		<div>
			<label>Music Name : </label>
			<input type="text" name="mname" value="<?php echo isset($mname)?$mname:""?>">
		</div>
		<div>
			<label>Music Year : </label>
			<input type="text" name="myear" value="<?php echo isset($myear)?$myear:""?>">
		</div>
		<div>
			<label>Music Genre: </label>
			<input type="text" name="mgenre" value="<?php echo isset($mgenre)?$mgenre:""?>">
		</div>
		<div>
			<input type="submit" value="search">
		</div>
	</form>
	<?php 
		if($searched){
			$sql = "select * from singer s,musics m ,genres g, musics_genre mg where s.id = m.singer and m.id = mg.musics_id and g.id = mg.genres_id ";
			if(isset($mname) and !empty($mname)){
				$sql = $sql." and m.name like '%".$mname."%'";
			}
			if(isset($myear) and !empty($myear)){
				$sql = $sql." and m.year = ".$myear;
			}
			if(isset($mgenre) and !empty($mgenre)){
				$sql = $sql." and g.name = '".$mgenre."'";
			}
			$con = getConn();
			$con->exec("set names utf8"); 
			$result = $con->query($sql); #echo $sql;
	?>
	<div class="feedback">
		<p>music name : <?php echo !empty($mname)?$mname:"\"\""?> , music year : <?php echo !empty($myear)?$myear:"\"\""?>, music genre : <?php echo !empty($mgenre)?$mgenre:"\"\""?><span> <?php echo $result->rowCount()?> results</span></p> 
	</div>
	<div class="results">
		<?php 
			foreach ($result as $resultrow){
				?>
				<div class="item">
					<a href="details.php?musicId=<?php echo $resultrow[4]?>"><?php echo $resultrow[5] ?></a>
				</div>
				<?php 
			}
		?>
	</div>
	<?php 
		}
	?>
	
</div>
</body>
</html>